package service.java;

public class Baldor {

    private Convierte convierte;

    public Baldor() {
        convierte = new Convierte();
    }

    public String suma(String... s) {
        String result = null;
        Double sum = 0D;
        boolean ok = true;

        for (int i = 0; i < s.length; i++) {
            Double x = convierte.aDouble(s[i]);

            if (x == null) {
                ok = false;
                break;
            }

            sum += x;
        }

        if (ok) {
            result = convierte.aString(sum);
        }

        return result;
    }

    public String resta(String s1, String s2) {
        String result = null;

        Double x1 = convierte.aDouble(s1);
        Double x2 = convierte.aDouble(s2);

        if ((x1 != null) && (x2 != null)) {
            result = convierte.aString(x1 + x2);
        }

        return result;
    }

    public String multiplica(String s1, String s2) {
        String result = null;

        Double x1 = convierte.aDouble(s1);
        Double x2 = convierte.aDouble(s2);

        if ((x1 != null) && (x2 != null)) {
            result = convierte.aString(x1 * x2);
        }

        return result;
    }

    public String divide(String s1, String s2) {
        String result = null;

        Double x1 = convierte.aDouble(s1);
        Double x2 = convierte.aDouble(s2);

        if ((x1 != null) && (x2 != null) && (x2 != 0D)) {
            result = convierte.aString(x1 / x2);
        }

        return result;
    }

    public String promedio(String... s) {
        String result = null;

        Double sum = 0D;
        boolean ok = true;

        for (int i = 0; i < s.length; i++) {
            Double x = convierte.aDouble(s[i]);

            if (x == null) {
                ok = false;
                break;
            }

            sum += x;
        }

        if (ok) {
            result = convierte.aString(sum / s.length);
        }

        return result;
    }

    public String maximo(String... s) {
        String result = null;
        Double max = Double.MIN_VALUE;
        boolean ok = true;

        for (int i = 0; i < s.length; i++) {
            Double x = convierte.aDouble(s[i]);

            if (x == null) {
                ok = false;
                break;
            }

            if (x > max) {
                max = x;
            }
        }

        if (ok) {
            result = convierte.aString(max);
        }

        return result;
    }
    
    public String minimo(String... s) {
        String result = null;
        Double min = Double.MAX_VALUE;
        boolean ok = true;

        for (int i = 0; i < s.length; i++) {
            Double x = convierte.aDouble(s[i]);

            if (x == null) {
                ok = false;
                break;
            }

            if (x < min) {
                min = x;
            }
        }

        if (ok) {
            result = convierte.aString(min);
        }

        return result;
    }
    
    public String igv(String monto) {
        String result = multiplica("0.18", monto);
        return result;
    }
}
